package com.twt.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twt.common.utils.MD5Util;
import com.twt.model.system.entity.SysUser;
import com.twt.common.pojo.ResultCodeEnum;
import com.twt.common.exceptions.AppException;
import com.twt.sys.mapper.SysUserMapper;
import com.twt.sys.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service
@Transactional("sysTransactionManager")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Override
    public SysUser registrySysUser(String account, String password) {
        Assert.hasText(account, "账号不能为空");
        Assert.hasText(password, "密码不能为空");
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        SysUser one = getOne(queryWrapper);
        if (one != null) {
            throw new AppException(ResultCodeEnum.ACC_HAS_EXITED);
        }
        SysUser sysUser = new SysUser();
        sysUser.setAccount(account);
        sysUser.setPassword(MD5Util.encode(password));
        sysUser.setUsername(account);
        save(sysUser);
        SysUser user = getOne(queryWrapper);
        return user;
    }
}
